package com.marketingexpert.controller.friendmgnt;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.marketingexpert.domain.UserType;
import com.marketingexpert.service.snsmgnt.SnsService;
import com.marketingexpert.service.snsmgnt.SnsServiceFactory;
import com.marketingexpert.service.usermgnt.UserService;
import com.marketingexpert.util.WebUtils;

public class FriendPorletController implements Controller {

private String successView;
	
private String tencentSuccessView;

private UserService userService;

private SnsServiceFactory snsServiceFactory;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		SnsService weboauth = null;
		
		String parentUserId = (String) request.getSession().getAttribute("parentUserId");
		
		List<com.marketingexpert.domain.User> myWeiboUsers = userService.queryUserByParentId(parentUserId);
		
		
		List<com.marketingexpert.service.snsmgnt.User> weiboUsers = new ArrayList<com.marketingexpert.service.snsmgnt.User>();
		
		for(Iterator<com.marketingexpert.domain.User> it = myWeiboUsers.iterator(); it.hasNext();) {
			com.marketingexpert.domain.User myUser = it.next();
			
			weboauth = WebUtils.getSnsService(myUser, snsServiceFactory);
			
			com.marketingexpert.service.snsmgnt.User webUser = weboauth.getUserDetails(myUser.getAccessToken(),myUser.getSecret(),myUser.getUserId());	
			
			weiboUsers.add(webUser);
		}	
		
		return new ModelAndView(getSuccessView(),"weiboUsers",weiboUsers);
	}

	public String getSuccessView() {
		return successView;
	}

	public void setSuccessView(String successView) {
		this.successView = successView;
	}

	public String getTencentSuccessView() {
		return tencentSuccessView;
	}

	public void setTencentSuccessView(String tencentSuccessView) {
		this.tencentSuccessView = tencentSuccessView;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public SnsServiceFactory getSnsServiceFactory() {
		return snsServiceFactory;
	}

	public void setSnsServiceFactory(SnsServiceFactory snsServiceFactory) {
		this.snsServiceFactory = snsServiceFactory;
	}
	
	

}
